home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicRadioButtonUI.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  224 lines

  1. /*
  2.  * @(#)BasicRadioButtonUI.java    1.44 98/06/22
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.java.swing.plaf.basic;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import com.sun.java.swing.*;
  20. import com.sun.java.swing.border.*;
  21. import com.sun.java.swing.plaf.*;
  22. import java.io.Serializable;
  23.  
  24.  
  25. /**
  26.  * RadioButtonUI implementation for BasicRadioButtonUI
  27.  * <p>
  28.  * Warning: serialized objects of this class will not be compatible with
  29.  * future swing releases.  The current serialization support is appropriate
  30.  * for short term storage or RMI between Swing1.0 applications.  It will
  31.  * not be possible to load serialized Swing1.0 objects with future releases
  32.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  33.  * baseline for the serialized form of Swing objects.
  34.  *
  35.  * @version 1.44 06/22/98
  36.  * @author Jeff Dinkins
  37.  */
  38. public class BasicRadioButtonUI extends BasicToggleButtonUI 
  39.     implements Serializable
  40. {
  41.     protected final static Insets defaultMargin = new Insets(2,2,2,2);
  42.  
  43.     protected Icon icon = null;
  44.  
  45.     public BasicRadioButtonUI() {
  46.     }
  47.  
  48.     protected static ToggleButtonUI radioButtonUI;
  49.  
  50.     public static ComponentUI createUI(JComponent b) {
  51.         if(radioButtonUI == null) {
  52.             radioButtonUI = new BasicRadioButtonUI();
  53.         }
  54.         return radioButtonUI;
  55.     }
  56.  
  57.  
  58.     /************************** The View *************************/
  59.  
  60.     ButtonModel model;
  61.  
  62.     protected void installDefaults(AbstractButton b){
  63.     super.installDefaults(b);
  64.     icon = createIcon();
  65.     LookAndFeel.installColorsAndFont(b,
  66.                      "RadioButton.background",
  67.                      "RadioButton.foreground",
  68.                      "RadioButton.font");
  69.     }   
  70.  
  71.  
  72.     /**
  73.      * paint the radio button
  74.      */
  75.     public synchronized void paint(Graphics g, JComponent c) {
  76.  
  77.     AbstractButton b = (AbstractButton) c;
  78.     model = b.getModel();
  79.     
  80.     Dimension size = c.getSize();
  81.  
  82.     int w = size.width;
  83.     int h = size.height;
  84.  
  85.     Font f = c.getFont();
  86.     g.setFont(f);
  87.     FontMetrics fm = g.getFontMetrics();
  88.  
  89.     Rectangle viewRect = new Rectangle(size);
  90.         Rectangle iconRect = new Rectangle();
  91.         Rectangle textRect = new Rectangle();
  92.  
  93.     Icon altIcon = b.getIcon();
  94.     Icon selectedIcon = null;
  95.     Icon disabledIcon = null;
  96.     
  97.         String text = SwingUtilities.layoutCompoundLabel(
  98.             fm, b.getText(), altIcon != null ? altIcon : icon,
  99.             b.getVerticalAlignment(), b.getHorizontalAlignment(),
  100.             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  101.             viewRect, iconRect, textRect, getDefaultTextIconGap(b)
  102.         );
  103.     
  104.         // fill background
  105.     if(c.isOpaque()) {
  106.         g.setColor(b.getBackground());
  107.         g.fillRect(0,0, size.width, size.height); 
  108.     }
  109.  
  110.     
  111.     // Paint the radio button
  112.         if(altIcon != null) { 
  113.  
  114.             if(!model.isEnabled()) {
  115.                 altIcon = b.getDisabledIcon();
  116.             } else if(model.isPressed() && model.isArmed()) {
  117.                 altIcon = b.getPressedIcon();
  118.                 if(altIcon == null) {
  119.                     // Use selected icon
  120.                     altIcon = b.getSelectedIcon();
  121.                 } 
  122.             } else if(model.isSelected()) {
  123.                 altIcon = b.getSelectedIcon();
  124.         } else if(b.isRolloverEnabled() && model.isRollover()) {
  125.                 altIcon = (Icon) b.getRolloverIcon();
  126.             } 
  127.           
  128.         if(altIcon == null) {
  129.         altIcon = b.getIcon();
  130.         }
  131.            
  132.         altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  133.  
  134.         } else {
  135.         icon.paintIcon(c, g, iconRect.x, iconRect.y);
  136.     }
  137.  
  138.  
  139.     // Draw the Text
  140.     if(text != null) {
  141.         if(model.isEnabled()) {
  142.         // *** paint the text normally
  143.         g.setColor(b.getForeground());
  144.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  145.                                               textRect.x, textRect.y + fm.getAscent());
  146.         } else {
  147.         // *** paint the text disabled
  148.         g.setColor(b.getBackground().brighter());
  149.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  150.                                               textRect.x + 1, textRect.y + fm.getAscent() + 1);
  151.         g.setColor(b.getBackground().darker());
  152.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  153.                                               textRect.x, textRect.y + fm.getAscent());
  154.         }
  155.             if(b.hasFocus() && b.isFocusPainted() && textRect.width > 0 && textRect.height > 0 ) {
  156.           paintFocus(g,textRect,size);
  157.  
  158.             }
  159.     }
  160.     }
  161.  
  162.     protected void paintFocus(Graphics g, Rectangle textRect, Dimension size){
  163.     }
  164.  
  165.     /**
  166.      * Creates the radio dot
  167.      */
  168.     public Icon createIcon() {
  169.     return UIManager.getIcon("RadioButton.icon");
  170.     }
  171.  
  172.  
  173.     /**
  174.      * The preferred size of the radio button
  175.      */
  176.     public Dimension getPreferredSize(JComponent c) {
  177.  
  178.     if(c.getComponentCount() > 0) {
  179.         return null;
  180.     }
  181.  
  182.         AbstractButton b = (AbstractButton) c;
  183.  
  184.         String text = b.getText();
  185.  
  186.         Icon radioIcon = (Icon) b.getIcon();
  187.     if(radioIcon == null) {
  188.         radioIcon = icon;
  189.     }
  190.  
  191.         int width = 0;
  192.         int height = 0;
  193.  
  194.         Font font = b.getFont();
  195.         FontMetrics fm = b.getToolkit().getFontMetrics(font);
  196.       
  197.         Rectangle iconR = new Rectangle();
  198.         Rectangle textR = new Rectangle();
  199.         Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
  200.  
  201.         SwingUtilities.layoutCompoundLabel(
  202.         fm, text, radioIcon,
  203.         b.getVerticalAlignment(), b.getHorizontalAlignment(),
  204.         b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  205.         viewR, iconR, textR, text == null ? 0 : getDefaultTextIconGap(b)
  206.     );
  207.  
  208.         // find the union of the icon and text rects
  209.         Rectangle r = iconR.union(textR);
  210.  
  211.     Insets insets = b.getInsets();
  212.     r.width += insets.left + insets.right;
  213.     r.height += insets.top + insets.bottom;
  214.  
  215.         return r.getSize();
  216.     }
  217.  
  218.     public Insets getDefaultMargin(AbstractButton b) {
  219.     return defaultMargin;
  220.     }
  221.  
  222.     
  223. }
  224.